/**
 * 手机号验证
 */

export const phoneVerification = (phone) => {
    let tip = ''
    if (!phone) {
        tip = '手机号不能为空';
    } else if (!/^1[3-9]\d{9}$/.test(phone)) {
        tip = '请输入正确的手机号'
    }
    return tip;
}

/**
 * 密码验证
 */
export const validatePassword = (password) => {
    if (!password) {
        return '密码不能为空';
    }
    if (password.length < 8 || password.length > 20) {
        return '密码长度应为8-20位';
    }
    // 检查是否包含数字、字母或符号
    const hasNumber = /\d/.test(password);
    const hasLetter = /[a-zA-Z]/.test(password);
    const hasSymbol = /[!@#$%^&*(),.?":{}|<>]/.test(password);
    
    if (!hasNumber && !hasLetter && !hasSymbol) {
        return '密码必须包含数字、字母或符号';
    }
    
    return '';
}

/**
 * 获取或修改globalData中的数据
 * 
 */

export const getAppEvent = (type,name,val) => {
    const app = getApp();
    if (type === 'get') {
        return app.globalData[name]
    } else if (type === 'set') {
        app.globalData[name] = val
    } else {
        throw new Error('获取或修改错误！')
    }
}

/**
 * 检查用户登录状态
 */
export const checkLoginStatus = () => {
    const token = uni.getStorageSync('token');
    return !!token;
}

/**
 * 跳转到登录页面
 */
export const goToLogin = () => {
    uni.navigateTo({
        url: '/pages/login/login'
    });
}

/**
 * 需要登录才能访问的页面检查
 */
export const requireLogin = (callback) => {
    if (checkLoginStatus()) {
        callback && callback();
    } else {
        uni.showModal({
            title: '提示',
            content: '请先登录',
            showCancel: false,
            success: () => {
                goToLogin();
            }
        });
    }
}

/**
 * 获取系统信息状态栏高度
 */
export const getSystemInfo = () => {
    const { statusBarHeight, system, screenWidth } = uni.getSystemInfoSync()
    let headerSafe = statusBarHeight * 2
    let minH = 0
    let padRight = 0
    // #ifdef MP-WEIXIN
    const { top, left, bottom, height } = uni.getMenuButtonBoundingClientRect()

    headerSafe = statusBarHeight + top
    minH = bottom + 4
    padRight = `${(screenWidth - left) * 2 + 40}rpx`
    // #endif
    headerSafe = headerSafe || 44
    padRight = padRight || '32rpx'
    return { headerSafe, minH, padRight }
}

/**
 * 格式化时间
 */
export const formatPublishTime = (time, format) => {
    const date = new Date(time * 1000)
    const y = date.getFullYear()
    const m = String(date.getMonth() + 1).padStart(2, '0')
    const d = String(date.getDate()).padStart(2, '0')
    const h = String(date.getHours()).padStart(2, '0')
    const min = String(date.getMinutes()).padStart(2, '0')
    return format.replace('YYYY', y).replace('MM', m).replace('DD', d).replace('HH', h).replace('mm', min)
}

/**
 * 数字格式化 k,w, 小数点后保留1位
 */
export const formatNumber = (num) => {
    if (num >= 10000) {
        return (num / 10000).toFixed(1) + 'W'
    } else if (num >= 1000) {
        return (num / 1000).toFixed(1) + 'K'
    } else {
        return num
    }
}
export let requestPayment = async (provider, orderInfo,  success, fail) => {
	if (provider == 'wxpay') {
		// #ifdef APP-PLUS
		uni.requestPayment({
			provider: 'wxpay',
			orderInfo: orderInfo,
			success: success,
			fail: fail
		});
		// #endif
		// #ifdef MP-WEIXIN
		console.log(2222);
		wx.requestPayment({
			"timeStamp": orderInfo.timeStamp,
			"nonceStr": orderInfo.nonceStr,
			"package": orderInfo.packageValue,
			"signType": orderInfo.signType,
			"paySign": orderInfo.paySign,
			success: success,
			fail: fail
		})

		// #endif
	} else if (provider == 'alipay') {
		uni.requestPayment({
			provider: 'alipay',
			orderInfo: orderInfo, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
			success: success,
			fail: fail
		});
	}
}
// module.exports = {
// 	requestPayment
// }